package com.tony.mindwarning.controller;

import com.tony.mindwarning.pojo.Type;
import com.tony.mindwarning.pojo.User;
import com.tony.mindwarning.service.TypeService;
import com.tony.mindwarning.utils.MessageResults;
import com.tony.mindwarning.utils.PageUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Title: TypeController
 * @Auther: 皮蛋布丁
 * @Date: 2021/04/08/10:14
 * @Description:
 */
@Controller
@Scope("prototype")
public class TypeController {

    @Resource
    TypeService typeService;

    /**
    * @Description: addType 新增商品类别
    * @Param: [type]
    * @return: com.tony.mindwarning.utils.MessageResults
    * @Author: 皮蛋布丁
    */
    @RequestMapping("/type_add")
    @ResponseBody
    public MessageResults addType(HttpSession session,Type type){
        MessageResults results=null;
        //对比类别名
        Integer i = typeService.compareTname(type.getTname());
        if (i != null) {
            results=new MessageResults(501,"新增失败",null);
            return results;
        }
        System.out.println("要新增的type对象是:"+type);
        User currUser= (User)session.getAttribute("userLogin");
        Integer count = typeService.addType(type,currUser.getId());
        if(count>0){
            results=new MessageResults(200,"新增成功",null);
        }else{
            results=new MessageResults(500,"新增失败",null);
        }
        return results;
    }


    /**
    * @Description: queryPage 分类分页
    * @Param: [pageIndex, pageSize, model]
    * @return: java.lang.String
    * @Author: 皮蛋布丁
    */
    @RequestMapping("/type_page")
    public String queryPage(@RequestParam(value = "pageIndex",defaultValue = "1") Integer pageIndex,
                             @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize, Model model){
        //总条数
        Integer totalCount = typeService.getTotalCount();
        Map<String,Object> map=new HashMap<String,Object>();
        //开始查询的位置
        map.put("startIndex",(pageIndex-1)*pageSize);
        map.put("pageSize",pageSize);
        //每页的数据
        List<Type> types = typeService.getTypesByPage(map);
        //封装数据
        PageUtils<Type> pageUtils=new PageUtils<Type>(pageIndex,pageSize,totalCount,types);
        System.out.println("分页工具类："+pageUtils);
        model.addAttribute("pageUtils",pageUtils);
        return "type";
    }

    /**
    * @Description: delete 类别删除
    * @Param: [tid]
    * @return: com.tony.mindwarning.utils.MessageResults
    * @Author: 皮蛋布丁
    */
    @ResponseBody
    @RequestMapping("/type_delete")
    public MessageResults delete(HttpSession session,Integer tid){
        User currUser= (User)session.getAttribute("userLogin");
        Integer count = typeService.deleteType(tid,currUser.getId());
        MessageResults results=null;
        if(count>0){
            results=new MessageResults(200,"删除成功",null);
        }else{
            results=new MessageResults(500,"删除失败",null);
        }
        return results;
    }

    /**
    * @Description: toupdate 跳转更新
    * @Param: [tid, model]
    * @return: java.lang.String
    * @Author: 皮蛋布丁
    */
    @RequestMapping("/type_getById")
    public String toupdate(Integer tid, Model model){
        Type type = typeService.getTypeById(tid);
        model.addAttribute("type",type);
        System.out.println("要更新的对象是:"+type);
        return "editType";
    }

    /**
    * @Description: update 更新
    * @Param: [type]
    * @return: com.tony.mindwarning.utils.MessageResults
    * @Author: 皮蛋布丁
    */
    @ResponseBody
    @RequestMapping("/type_update")
    public MessageResults update(HttpSession session,Type type){
        User currUser= (User)session.getAttribute("userLogin");
        System.out.println("要更新的对象是："+type);
        MessageResults results=null;
        try {
            int count = typeService.updateType(type,currUser.getId());
            if(count>0){
                results=new MessageResults(200,"更新成功",null);
            }else{
                results=new MessageResults(500,"更新失败",null);
            }
        } catch (Exception e) {
            results=new MessageResults(500,"更新失败",null);
            e.printStackTrace();
        } finally {
        }
        return results;
    }

    /**
    * @Description: findTypes 返回商品类别的JSON数据
    * @Param: []
    * @return: java.util.List<com.tony.mindwarning.pojo.Type>
    * @Author: 皮蛋布丁
    */
    @ResponseBody
    @RequestMapping("/type_all")
    public List<Type> findTypes(){
        return typeService.getTypes();
    }

}
